<template>
  <div>
    <!-- 添加或修改系统应用类型对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body v-dialogDrag>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="类型名称" prop="typeName">
          <el-input v-model="form.typeName" :placeholder="watchEdit === 'watch' ? '' : '请输入'" />
        </el-form-item>
        <el-form-item label="类型编码" prop="typeCode">
          <el-input v-model="form.typeCode" :disabled="form.id !== null" :placeholder="watchEdit === 'watch' ? '' : '请输入'" />
        </el-form-item>
        <el-form-item label="描述" prop="remark">
          <el-input v-model="form.remark" type="textarea" :placeholder="watchEdit === 'watch' ? '' : '请输入'" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="addLoading" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAppType, getAppType, delAppType, addAppType, updateAppType } from "@/api/system/appType";

export default {
  name: "AppTypeAddOrUpdate",
  props: {
    watchEdit: {
      type: String,
      default: 'add'
    }
  },
  data() {
    return {
      // 遮罩层
      addLoading: false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        typeName: [
          { required: true, message: "类型名称不能为空", trigger: "blur" }
        ],
        typeCode: [
          { required: true, message: "类型编码不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {},
  methods: {
    init(id) {
      this.title = "添加应用类型";
      this.open = true
      this.reset();
      if (id) {
        this.title = "修改应用类型";
        getAppType(id).then(response => {
          this.form = response.data;
        });
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        typeName: null,
        typeCode: null,
        delFlag: null,
        isDefault: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.addLoading = true
          let r
          if (this.form.id != null) {
            r = updateAppType(this.form);
          } else {
            r = addAppType(this.form);
          }
          r.then((res) => {
            this.$modal.msgSuccess("操作成功");
            this.open = false;
            this.$emit('refreshDataList');
          }).finally(()=>{
            this.addLoading = false
          })
        }
      });
    }
  }
};
</script>
